/**
 * 
 */
package app.tuwien.isi.tweetanalyser;



import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author Philipp Pospisil
 *
 */
public class SmallStatisticActivity extends Activity{
	private String[] item;
	private int[] sum;
	List<String[]> statistic;
	ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistic);
	    
		statistic = new ArrayList<String[]>();
		
		Intent intent = getIntent();
		
		if(intent.getStringArrayExtra("item") == null){
			Log.e("Languages","is null at Language Statistic Activity");
		}else{
			item = intent.getStringArrayExtra("item");

			if(intent.getIntArrayExtra("sum") == null){
				Log.e("sum","is null at Language Statistic Activity");
			}else{
				sum = intent.getIntArrayExtra("sum");
				
				listView = (ListView) findViewById(R.id.statisticList);
				
				for(int i=0; i < item.length; i++){
					String[] temp = new String[]{item[i],""+sum[i]};
					statistic.add(temp);
				}
				
				for(int n=statistic.size(); n>1; n=n-1){
					String[] temp = new String[2];
					for (int i=0; i<n-1; i=i+1){
		    			if (Integer.parseInt(statistic.get(i)[1]) < Integer.parseInt(statistic.get(i+1)[1])){
		    				temp = statistic.get(i);
		    				statistic.set(i, statistic.get(i+1));
		    				statistic.set(i+1, temp);
		    			}
		    		}
				}
				
				listView.setAdapter(new SmallStatisticAdapter(SmallStatisticActivity.this, statistic));
			}
		}
		
		if(intent.getStringExtra("query") != null){
			listView.setOnItemClickListener(new OnItemClickListener() {
			    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
			    	Intent intent = new Intent (getApplicationContext(), MainActivity.class);
			    	intent.putExtra("query", "#" + statistic.get(position)[0]);  	
			    	startActivityForResult(intent, 1);
			    }
			});
		}
	}
	
	
	
	public class SmallStatisticAdapter extends BaseAdapter {

		private final List<String[]> statistic;
	    private LayoutInflater statisticInflater;
	    
	    public SmallStatisticAdapter(Context context, List<String[]> statistic) {
	        this.statistic = statistic;

	        statisticInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    }
	    
	    public int getCount() {
	        return statistic.size();
	    }
		public long getItemId(int position) {
			return position;
		}
	    public Object getItem(int position) {
	        return statistic.get(position);
	    }
	    public String getItemName(int position) {
	        return statistic.get(position)[0];
	    }
	    public String getItemSum(int position) {
	        return statistic.get(position)[1];
	    }
	    
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	String[] stat = (String[]) getItem(position);	
	        
	    	ViewHolder holder;
	        
	        if(convertView == null) {
	        	convertView = statisticInflater.inflate(R.layout.statistic_item, parent, false);

	        	holder = new ViewHolder(); 
	        	holder.textCategory = (TextView) convertView.findViewById(R.id.categoryText); 
	   		 	holder.textSum = (TextView) convertView.findViewById(R.id.sumText); 
	   		 	
	   		 	convertView.setTag(holder); 
	        } else {
	        	holder = (ViewHolder) convertView.getTag(); 
	        }

	        holder.textCategory.setText(stat[0]); 
	        holder.textSum.setText(stat[1]);
	        
	        return convertView;
	    }
	    
	    public class ViewHolder {
	        public TextView textCategory;
	        public TextView textSum;
	    }
	}
}
